var pagenotification=1;
var pagenotification2=1;
var filternotification=3;
var filternotification2=3;
function setpopupmsg(msg,notiid,displayname)
{

        $.post(serviceinfo+'readnotification', { notiid:notiid }, function(data) { 
        $(".wrpPopBcLkTxt #broadcastyou").attr("href",baseinfo+'/'+$("#wrpNotiBox-"+notiid+" .usernameset").val()) ;
       $(".wrpPopBcLkTxt .genImgUser.bcImgUser").attr("src",$("#wrpNotiBox-"+notiid+" .genImgUser").attr("src"));
       $(".wrpPopBcLkTxt .genNameUserFull.bcNameUser").html(displayname);
       $(".wrpPopBcLkTxt .frmTxtArea.frmGen").val(msg);
       $(".wrpPopBcLkTxt .bcTime").html($("#wrpNotiBox-"+notiid+" .timeNoti").html());
       $(".wrpPopBcLkTxt").fadeIn();     
       $("#wrpNotiBox-"+notiid).removeClass('notiAtv');

});
          

}
function getnotificationlist()
{
$.post(serviceinfo+'getmynotificationlist', { page:pagenotification,filter:filternotification }, function(data) { 

if(data.data.length>0)
 {
 if(data.data.length!=20)
 {
 $('.wrpSecNoti .btnSeeMore').hide();
 
 }else
 {
$('.wrpSecNoti .btnSeeMore').show(); 
 }
 var code="";
 var msgset="";
 var classread="";
 var newpathnow="";
 var onclick;
       $.each(data['data'], function(key, value) {
classread="";       
if(value.read==0)
{
classread="notiAtv";

}   
newpathnow= value.fromuser;
if(typeof value.touser!= 'undefined'){
if(useridinfo==value.fromuser.userid)
{
  newpathnow=value.touser;

}                   
}
if(value.ismsg==1)
{

onclick='setpopupmsg(\''+value.msg+'\',\''+value.notiid+'\',\''+newpathnow.displayname+'\')';
}else
{
onclick='readnotification('+value.notiid+',\''+value.linkto+'\')';

}
var condition='';
if(value.condition)
{
condition=value.condition;
}
//console.log(newpathnow);
//console.log(newpathnow.simguser.newpath);
code += '<div class="wrpNotiBox '+classread+'" id="wrpNotiBox-'+value.notiid+'" onclick="'+onclick+'">\n';
code += '<div class="notiBox">\n';

code += '<img width="24" height="24" alt="img" src="'+newpathnow.simguser.newpath+'" class="genImgUser">\n';
code += '<div class="ctntNoti">\n';
code += '<div class="nameAction">\n';
code += '<span class="genNameUserFull clTxt2 fs02">'+newpathnow.displayname+'</span>\n';

code +='<input type="hidden" class="usernameset" value="'+newpathnow.username+'" >';

code += '<span class="actnNoti clTxt3 fs02">'+condition+'</span>\n';
code += '</div>\n';

code += '<div class="wrpMsgNoti clTxt3 fs02">\n';

if(typeof value.msg!= 'undefined'){
if(value.link)
{
 msgset=value.link;
}else
{
 msgset=value.msg;
}
code += '"<span class="msgNoti cutTxt">'+msgset+'</span>"\n';
}

code += '</div>\n';
code += '<div class="wrpTimeViaNoti clTxt3 fs00">\n';
code += '<span class="timeNoti" title="'+value.createdate+'">'+value.createdate+'</span> \n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';

       });
       $('.wrpInNoti.clBd2.bdBtm').append(code);
       $("span.timeNoti").timeago(); 
       pagenotification++;
 }


});


}

function getcount2cart()
{
 
$.get(serviceinfo+'getcount2cart', { }, function(data) { 

    if(data.data)
     {
     $('#cartlinkbtn').html('Cart('+data.data+')');
     }

});

}

function getDisplayDate(year, month, day) {
    today = new Date();
    today.setHours(0);
    today.setMinutes(0);
    today.setSeconds(0);
    today.setMilliseconds(0);
    compDate = new Date(year,month-1,day); // month - 1 because January == 0
    diff = today.getTime() - compDate.getTime(); // get the difference between today(at 00:00:00) and the date
    if (compDate.getTime() == today.getTime()) {
        return "Today";
    } else if (diff <= (24 * 60 * 60 *1000)) {
        return "Yesterday";
    } else { 
    
        return day+'/'+month+'/'+year; // or format it what ever way you want
    }
}
function getnotificationlistpage()
{
var stringtemp='';
var code='';
var newpathnow;
var msgset='';
var onclick;
$.post(serviceinfo+'getmynotificationlist', { page:pagenotification2,filter:0,dateset:1 }, function(data) { 

if(data.data.length>0)
 {
        $.each(data['data'], function(key, value) {
       
        var res = value.createdate.split(" "); 
        var res2 = res[0].split("-"); 
        
        var stringdata=getDisplayDate(parseInt(res2[0],10), parseInt(res2[1],10), parseInt(res2[2],10));
        
        if(stringdata!=stringtemp)
        {
        stringtemp=stringdata;
        code += '<div class="wrpDateNotiP clBd2 bdBtm">\n';
        code += '<span class="boldGen clTxt2 fs04">'+stringdata+'</span>\n';
        code += '</div>';
        
        
        }
        
        newpathnow= value.fromuser;
if(typeof value.touser!= 'undefined'){
if(useridinfo==value.fromuser.userid)
{
  newpathnow=value.touser;

}                   
}
var condition='';
if(value.condition)
{
condition=value.condition;
}
        var res3 = res[1].split(":"); 
        
        
 var   classread="";       
if(value.read==0)
{
classread="notiAtv";

}   

if(value.ismsg==1)
{

onclick='setpopupmsg(\''+value.msg+'\',\''+value.notiid+'\',\''+newpathnow.displayname+'\')';
}else
{
onclick='readnotification('+value.notiid+',\''+value.linkto+'\')';

}
        
        code += '<div class="wrpNotiBox clBd2 bdBtm '+classread+'" onclick="'+onclick+'">\n';
        code += '<div class="notiBox clearfix">\n';
        code += '<img width="24" height="24" class="genImgUser" src="'+newpathnow.simguser.newpath+'" alt="img">\n';
        code += '<div class="nameAction">\n';
        code += '<span class="genNameUserFull clTxt2 fs02">'+newpathnow.displayname+'</span>\n';
        code += '<span class="actnNoti clTxt3 fs02">'+condition+'</span>\n';
        code += '</div>\n';
        code += '<div class="wrpMsgNoti clTxt3 fs02">\n';
        
        if(typeof value.msg!= 'undefined'){
if(value.link)
{
 msgset=value.link;
}else
{
 msgset=value.msg;
}
code += '<span class="msgNoti cutTxt">'+msgset+'</span>\n';
}
        
        //code += '<span class="msgNoti cutTxt">"Welcome to INFOSTANT."</span>\n';
        code += '</div>\n';
        code += '<div class="wrpTimeViaNoti clTxt3 fs00">\n';
        code += '<span class="timeNoti" title="'+res3[0]+":"+res3[1]+'">'+res3[0]+":"+res3[1]+'</span>\n';
        code += '</div>\n';
        code += '</div>\n';
        code += '</div>';
        

        });
$('.wrpInNotiP').html(code);

 }


});


}

function readnotification(notiid,linktoset)
{
$.post(serviceinfo+'readnotification', { notiid:notiid }, function(data) { 

    location.href=linktoset;


});

}

function readallnotification()
{
$("#btnNotiRight span.notiNum.clTxtW.fs01").html(0); 
$.post(serviceinfo+'readallnotification', { }, function(data) { 

   
$(".wrpNotiBox").removeClass('notiAtv');

});

}


function countnotification()
{
$.post(serviceinfo+'countnotification', function(data) { 
$(".notiNum.clTxtW.fs01").html(data.data.countdata);
if(data.data.countdata==0)
{
                 $("#btnNotiRight .notiDef.clBgBt1").removeClass("clBgSp2");
                 
}else
{
                  $("#btnNotiRight .notiDef.clBgBt1").removeClass("clBgSp2"); 
                   $("#btnNotiRight .notiDef.clBgBt1").addClass("notiCl"); 
                  

}
});

}
function swapnotification(filter)
{
filternotification=filter;
pagenotification=1;
$('.wrpInNoti.clBd2.bdBtm').html('');
getnotificationlist();


}